<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

class VirtualLabs_Admin_Rol extends VirtualLabs_Admin_Dao {

    const TABLE_NAME = 'rol';

    /**
     * Guarda los datos de un rol en base de datos.
     * En caso $codigoRol este seteado se realiza un update utilizando 
     * este valor como condicion de busqueda, caso contrario realiza 
     * un insert en la base de datos.
     * 
     * @param type $data datos de la tabla 'rol' (codigo_rol,nombre)
     * @throws Exception
     * @return array los valores almanados en caso de realizar las operaciones correctamente
     */
    public function save($data, $codigoRol = null) {
        try {
            $db = Zend_Db_Table::getDefaultAdapter();
            if (!is_null($codigoRol)) {
                $db->update(VirtualLabs_Admin_Rol::TABLE_NAME, $data, $codigoRol);
            } else {
                $db->insert("rol", $data);
                $data['codigo_rol'] = $db->lastInsertId("rol");
            }
            return $data;
        } catch (Exception $e) {
            $logger = Zend_Registry::get(VirtualLabs_Common::DEBUG_LOG);
            $logger->debug($e->getMessage());
            $logger->debug($e->getTraceAsString());
            throw $e;
        }
    }

    /**
     * Metodo que devuelve roles segun un criterio.
     * @param type $fields
     * @param type $where
     * @return type
     * @throws Exception
     */
    public function getRoles($fields = array(), $where = array()) {
        $db = Zend_Db_Table::getDefaultAdapter();
        $select = $db->select()->from("rol", $fields);
        foreach ($where as $key => $value) {
            $select->where("$key {$value['criteria']} ?", $value['value']);
        }
        try {
            return $db->fetchAll($select);
        } catch (Exception $e) {
            $logger = Zend_Registry::get(VirtualLabs_Exception::ERROR_LOG);
            $logger->err($e->getMessage());
            $logger->err($e->getTraceAsString());
            throw $e;
        }
    }

    public function getALLRoles() {
        $db = Zend_Db_Table::getDefaultAdapter();
        $select = $db->select()->from("rol", '*');
        try {
            return $db->fetchAll($select);
        } catch (Exception $e) {
            $logger = Zend_Registry::get(VirtualLabs_Exception::ERROR_LOG);
            $logger->err($e->getMessage());
            $logger->err($e->getTraceAsString());
            throw $e;
        }
    }

    /**
     * 
     * @param type $fields
     * @param type $where
     * @return type
     * @throws Exception
     */
    public function getRol($fields = array(), $where = array()) {
        $db = Zend_Db_Table::getDefaultAdapter();
        $select = $db->select()->from("rol", $fields);
        foreach ($where as $key => $value) {
            $select->where("$key {$value['criteria']} ?", $value['value']);
        }

        try {
            return $db->fetchRow($select);
        } catch (Exception $e) {
            $logger = Zend_Registry::get(VirtualLabs_Exception::ERROR_LOG);
            $logger->err($e->getMessage());
            $logger->err($e->getTraceAsString());
            throw $e;
        }
    }

    public function isExistRolPermiso($codigo_rol, $codigo_permiso) {
        $retval = false;
        $db = Zend_Db_Table::getDefaultAdapter();
        $select = $db->select()->from("permiso_rol", '*');
        $where = array("codigo_rol" => array("criteria" => "=", "value" => $codigo_rol),
            "codigo_permiso" => array("criteria" => "=", "value" => $codigo_permiso));
        foreach ($where as $key => $value) {
            $select->where("$key {$value['criteria']} ?", $value['value']);
        }
        try {
            $result = $db->fetchAll($select);

            if (count($result) > 0) {
                $retval = true;
            }
            return $retval;
        } catch (Exception $e) {
            $logger = Zend_Registry::get(VirtualLabs_Exception::ERROR_LOG);
            $logger->err($e->getMessage());
            $logger->err($e->getTraceAsString());
            throw $e;
        }
    }

    /* Elimina los permisos asociados al codigo de rol en la tabla PERMISO_ROL
     * 
     * @param $code array(index, value)
     * 
     */
    public function deletePermisosAsociados($code) {

        try {
            $this->delete(VirtualLabs_Admin_Permiso::TABLE_NAME, $code);
            return true;
        } catch (Exception $e) {
            $logger = Zend_Registry::get(VirtualLabs_Exception::ERROR_LOG);
            $logger->err($e->getMessage());
            $logger->err($e->getTraceAsString());
            throw $e;
        }
    }

    public function insertPermisoAsociado($codigoRol, $codigoPermiso) {
        try {
            $data = array(
                'codigo_rol' => $codigoRol,
                'codigo_permiso' => $codigoPermiso
            );
            $db = Zend_Db_Table::getDefaultAdapter();

            $debug = Zend_Registry::get(VirtualLabs_Common::DEBUG_LOG);
            $debug->debug("Insertando permiso_rol: " . print_r($data, true));

            $db->insert("permiso_rol", $data);
            return true;
        } catch (Exception $e) {
            $logger = Zend_Registry::get(VirtualLabs_Exception::ERROR_LOG);
            $logger->err($e->getMessage());
            $logger->err($e->getTraceAsString());
            throw $e;
        }
    }

}

?>
